<template>
  <el-pagination
    v-bind="$attrs"
    background
    :page-size="pagination.pageSize"
    :total="pagination.total"
    :current-page="pagination.currentPage"
    :page-sizes="pageSizes"
    :layout="layout"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
  >
  </el-pagination>
</template>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    pagination: {
      type: Object,
      default: () => {
        return {
          currentPage: 1,
          pageSize: 10,
          total: 100
        }
      }
    },
    layout: {
      type: String,
      default: 'sizes, prev, pager, next, total'
    },
    pageSizes: {
      type: Array,
      default: () => [10, 20, 50]
    }
  },
  emits: ['pageChange'],
  setup(_, context) {
    const handleSizeChange = (pageSize: number) => {
      context.emit('pageChange', { key: 'pageSize', value: pageSize })
    }

    const handleCurrentChange = (currentPage: number) => {
      context.emit('pageChange', { key: 'currentPage', value: currentPage })
    }

    return {
      handleSizeChange,
      handleCurrentChange
    }
  }
})
</script>
